package com.sjd.farm.service.impl;

import com.sjd.common.utils.DateUtils;
import com.sjd.farm.domain.SjdFarmLand;
import com.sjd.farm.domain.SjdFarmLandChange;
import com.sjd.farm.mapper.SjdFarmLandMapper;
import com.sjd.farm.service.ISjdFarmLandChangeService;
import com.sjd.farm.service.ISjdFarmLandService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 农场土地信息Service业务层处理
 *
 * @author joker
 * @date 2022-01-24
 */
@Service
@AllArgsConstructor
public class SjdFarmLandServiceImpl implements ISjdFarmLandService {
    private final SjdFarmLandMapper sjdFarmLandMapper;
    private final ISjdFarmLandChangeService changeService;

    /**
     * 查询农场土地信息
     *
     * @param landId 农场土地信息主键
     * @return 农场土地信息
     */
    @Override
    public SjdFarmLand selectSjdFarmLandByLandId(long landId) {
        return sjdFarmLandMapper.selectSjdFarmLandByLandId(landId);
    }

    /**
     * 查询农场土地信息列表
     *
     * @param sjdFarmLand 农场土地信息
     * @return 农场土地信息
     */
    @Override
    public List<SjdFarmLand> selectSjdFarmLandList(SjdFarmLand sjdFarmLand) {
        return sjdFarmLandMapper.selectSjdFarmLandList(sjdFarmLand);
    }

    /**
     * 新增农场土地信息
     *
     * @param sjdFarmLand 农场土地信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSjdFarmLand(SjdFarmLand sjdFarmLand) {
        int rows = sjdFarmLandMapper.insertSjdFarmLand(sjdFarmLand);
        changeService.insertSjdFarmLandChange(
                SjdFarmLandChange.builder().landId(sjdFarmLand.getLandId())
                        .changeType(SjdFarmLandChange.Param.CHANGE_TYPE_INIT)
                        .changeTime(DateUtils.getTime())
                        .changeContent("新增土地信息" + sjdFarmLand)
                        .build()
        );
        return rows;
    }

    /**
     * 修改农场土地信息
     *
     * @param sjdFarmLand 农场土地信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSjdFarmLand(SjdFarmLand sjdFarmLand) {
        changeService.insertSjdFarmLandChange(
                SjdFarmLandChange.builder().landId(sjdFarmLand.getLandId())
                        .changeType(SjdFarmLandChange.Param.CHANGE_TYPE_CHANGE)
                        .changeTime(DateUtils.getTime())
                        .changeContent("变更土地信息" + sjdFarmLand)
                        .build()
        );
        return sjdFarmLandMapper.updateSjdFarmLand(sjdFarmLand);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int batchHandleStatus(String landFlag, int oldStatus, int newStatus) {
        if ("all".equals(landFlag)) {
            // 批量上下架
            return sjdFarmLandMapper.batchHandleStatus(oldStatus, newStatus);
        } else {
            // 单个上下架
            long landId = Long.parseLong(landFlag);
            changeService.insertSjdFarmLandChange(
                    SjdFarmLandChange.builder().landId(landId)
                            .changeType(SjdFarmLandChange.Param.CHANGE_TYPE_CHANGE)
                            .changeTime(DateUtils.getTime())
                            .changeContent("变更土地信息上下架状态，oldStatus为"+oldStatus+"，newStatus为"+newStatus)
                            .build()
            );
            return sjdFarmLandMapper.updateSjdFarmLand(SjdFarmLand.builder().landId(landId).landStatus(newStatus).build());
        }
    }

}
